home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-04 | 10.7 KB | 415 lines | [TEXT/MMCC] |
-
- //• Subject: Display a file program
- //• Message-ID: <1701@uw-beaver>
- //• Date: Wed, 6 Nov 85 22:18:16 MST
- //• Date-Received: Fri, 8 Nov 85 02:05:35 MST
- //• Sender: daemon@uw-beaver
- //• Organization: U of Washington Computer Science
- //• Lines: 372
-
- //• From: kangaro!milo@Purdue.EDU
-
- //• This is a little program I whipped up in Megamax C that will
- //• display any Mac file of type TEXT. It is very handy for looking
- //• at text files you have downloaded from a BBS without having to
- //• load a text editor. The program loads in about 4 seconds and
- //• supports standard Mac desk accessorys...etc.
-
- //• Let me know if you have any problems with the program...the
- //• source follows...
-
- //• Greg Corson
- //• UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
- //• ARPA: pur-ee!kangaro!milo@Purdue.ARPA
- //• EDU: kangaro!milo@ee.Purdue.EDU
- //• Or call my BBS at (219) 277-5825
-
- //• This sample program allows a user to open a file and print
- //• it's contents to a window on the Mac screen. To give the
- //• compiled program an Icon copy the resources from Display.rsrc
- //• (use Rmover or Resource Editor) and paste them into the
- //• compiled file. Then use SetFile to change the creator name of
- //• the compiled file to DFIL and set the bundle bit. When you
- //• return to the finder the program icon should have an icon.
-
- //• By Greg Corson
- //• 19141 Summers Drive
- //• South Bend, IN 46637
- //• (219) 272-2136
- //• UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
- //• ARPA: pur-ee!kangaro!milo@Purdue.ARPA
- //• EDU: kangaro!milo@ee.Purdue.EDU
- //• Or call my BBS at (219) 277-5825
-
- #include <packages.h>
-
- #define lastMenu 4
- #define appleMenu 1
- #define fileMenu 256
- #define editMenu 257
- #define stopMenu 258
-
- RgnHandle updateRgn;
- MenuHandle myMenus[lastMenu+1];
- Rect screenRect, dragRect, pRect;
- Boolean doneFlag, temp;
- EventRecord myEvent;
- short code, refNum;
- WindowRecord wRecord;
- WindowPtr myWindow, whichWindow;
- GrafPtr temPort;
- short theMenu, theItem;
- short fileOpen, howWide, fd1;
- long count;
- char tempBuf[32];
-
- void Center (char *str);
-
- //•---------------------------------------------------------------------------*/
- //• This subroutine processes commands from the menu bar, where
- //• theMenu is the menu ID, theItem is the item number in the menu.
-
- void DoCommand (long mSelect)
- {
- short theMenu = HiWord(mSelect);
- short theItem = LoWord(mSelect);
- char name[256];
- SFReply rep;
- SFTypeList typeList;
- short i;
-
- Point openP = { 100, 60 };
-
- //• Switch to decide what menu the cursor is in.
- switch (theMenu)
- {
- //• Mouse down in apple menu.
- case appleMenu:
- //• Item one is the "about Display a file" box.
- if (theItem == 1)
- {
- TextFont (systemFont);
- TextSize (12);
- EraseRect (&pRect);
- MoveTo (pRect.left, pRect.top+70);
- Center ((char*) "\pDisplay a file program");
- Center ((char*) "\pCopyright 1985 by Greg Corson");
- Center ((char*) "\pKangaroo Koncepts, Inc.");
- Center ((char*) "\p19141 Summers Drive");
- Center ((char*) "\pSouth Bend, IN 46637");
- Center ((char*) "\p1(219) 277-5306");
- TextFont (monaco);
- TextSize (9);
- Move (0, -3);
- Center ((char*) "\pFeel free to give this program away to all your friends.");
- Center ((char*) "\pIt should NOT be sold for profit. Be sure to try our");
- Center ((char*) "\pComputer Based Communications System \"The Connection\"");
- Center ((char*) "\pFree demo line (219) 277-5825 available 24 hours at 300 or");
- Center ((char*) "\p1200 baud. Be sure to look at the \"MacTech\" special");
- Center ((char*) "\pinterest group for information of interest to Mac");
- Center ((char*) "\pprogrammers and the \"macintosh\" SIG for general info.");
- Center ((char*) "\pAnother old program \"brought back from the dead\"");
- Center ((char*) "\pby Ken Long, at itty bitty bytes™");
- Pretty ();
- MoveTo (pRect.left + 1, pRect.bottom - 2);
- }
- //• The rest of the items are desk accessorys.
- else
- {
- GetItem (myMenus[1], theItem, (StringPtr) name);
- refNum = OpenDeskAcc ((StringPtr) name);
- SetPort (myWindow);
- }
- break;
-
- case fileMenu:
- //• Mouse down in file menu.
- switch (theItem)
- {
- case 1: //• Open file.
- typeList[0] = 'TEXT';
- SFGetFile (openP, "\p", 0L, 1, typeList, 0L, &rep);
- if (rep.good)
- {
- //• Doesn't mess with DisableItem, etc.
- if (fileOpen)
- {
- FSClose (fd1);
- fileOpen = false;
- }
- if (FSOpen (rep.fName, rep.vRefNum, &fd1) == noErr)
- {
- ScrollRect (&pRect, 0, -11, updateRgn);
- fileOpen = true;
- }
- }
- break;
-
- //• Close file.
- case 3:
- if (fileOpen)
- {
- FSClose (fd1);
- fileOpen = FALSE;
- }
- break;
-
- //• Quit.
- case 5:
- if (fileOpen)
- {
- FSClose (fd1);
- fileOpen = FALSE;
- }
- doneFlag = 1;
- break;
- }
- break;
-
- case editMenu: //• Process system edit events.
- SystemEdit (theItem - 1);
- break;
- }
- HiliteMenu (0);
- }
-
- //•---------------------------------------------------------------------------*/
- void main (void)
- {
- short i, j;
- char keyChar;
-
- //• Initialize variables.
-
- j = 0;
- doneFlag = false;
- fileOpen = false;
-
- //• Initialize quickdraw, fonts, events, windows, menus,
- //• dialogs and cursor.
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (0L);
- InitCursor ();
-
- //• Create an empty region pointer for use by scrollrect later.
- updateRgn = NewRgn ();
-
- //• Setup the menu bar.
- SetUpMenus ();
-
- //• Setup the drag rectangle so part of the window will always
- //• be visible.
- SetRect (&screenRect, 4, 40, 508, 338);
- SetRect (&dragRect, 4, 24, screenRect.right - 4,
- screenRect.bottom - 4);
-
- //• Create the window and set the current port to the window port.
- myWindow = NewWindow (&wRecord, &screenRect, "\pDisplay a file",
- true, 0, (WindowPtr)-1L, false, (long) 0);
- SetPort (myWindow);
-
- //• get the rectangle for the current window and put it in pRect.
- BlockMove (&qd.thePort->portRect, &pRect, (long)sizeof pRect);
- howWide = pRect.right - pRect.left;
-
- //• Now that the window and menus are drawn set the window font
- //• to monaco 9.
-
- TextFont (monaco);
- TextSize (9);
- MoveTo (pRect.left + 1, pRect.bottom - 2);
-
- //• Main loop to process events.
- do
- {
- //• If a file is open copy a line to the output window.
- if (fileOpen)
- {
- count = 32;
- FSRead (fd1, &count, tempBuf);
- if (count == 0)
- {
- FSClose (fd1);
- fileOpen = false;
- MoveTo (pRect.left + 1, pRect.bottom - 2);
- ScrollRect (&pRect, 0, -11, updateRgn);
- DrawString ("\p-------End of File-------");
- ScrollRect (&pRect, 0, -11, updateRgn);
- MoveTo (pRect.left + 1, pRect.bottom - 2);
- }
- else
- {
- for (i = 0; i < count; i++)
- {
- if (tempBuf[i] > 31)
- DrawChar (tempBuf[i]);
- else
- {
- //• Scroll window if we get a carriage
- //• return.
- if (tempBuf[i] == '\r')
- {
- j = 0;
- ScrollRect (&pRect, 0, -11, updateRgn);
- MoveTo (pRect.left+1, pRect.bottom - 2);
- }
- //• Expand tabs by outputting spaces.
- if (tempBuf[i] == '\011')
- {
- DrawChar (' ');
- j++;
- for (; j & 07; j++)
- DrawChar (' ');
- }
- }
- }
- }
- }
- //• Get the next event.
- SystemTask ();
- temp = GetNextEvent (everyEvent, &myEvent);
- switch (myEvent.what)
- {
- //• mouse down, call findwindow to figure out where.
- case mouseDown:
- code = FindWindow (myEvent.where, &whichWindow);
- switch (code)
- {
- //• in meun bar, execute the menu command.
- case inMenuBar:
- DoCommand (MenuSelect (myEvent.where));
- // HandleMenu(MenuSelect(theEvent->where));
- break;
-
- //• in desk accessory, call desk manager.
- case inSysWindow:
- SystemClick (&myEvent, whichWindow);
- break;
-
- //• in drag, call dragwindow to move it.
- case inDrag:
- DragWindow (whichWindow, myEvent.where, &dragRect);
- break;
-
- //• In content area, make application window the
- //• frontmost.
- case inContent:
- if (whichWindow != FrontWindow ())
- SelectWindow (whichWindow);
- break;
- }
- break;
-
- //• If keydown event, check for menu command key.
- case keyDown:
- if (myEvent.modifiers & cmdKey)
- {
- keyChar = myEvent.message & charCodeMask;
- DoCommand (MenuKey (keyChar));
- }
- break;
-
- case autoKey:
- break;
-
- //• Application window becomming active, do nothing.
- case activateEvt:
- if ((myEvent.modifiers & 1) &&
- (((WindowPtr)myEvent.message) == myWindow))
- {
- DisableItem (myMenus[3], 0);
- EnableItem (myMenus[2], 0);
- DrawMenuBar ();
- }
- else
- {
- EnableItem (myMenus[3], 0);
- DisableItem (myMenus[2], 0);
- DrawMenuBar ();
- }
- break;
-
- //• Update event, update the window frame.
- case updateEvt:
- if (((WindowPtr)myEvent.message) == myWindow)
- {
- BeginUpdate (myWindow);
- EndUpdate (myWindow);
- }
- break;
- }
- } while (doneFlag == 0);
- }
-
- //•---------------------------------------------------------------------------*/
- //• SetUpMenus ()---This subroutine sets up the menu bar and
- //• reads in the desk accessory menu.
-
- SetUpMenus ()
- {
- short i;
-
- //• Apple menu, \024 is the apple character, adresmenu call
- //• loads all type DRVR resources.
- myMenus[1] = NewMenu (appleMenu, "\p\024");
- AppendMenu (myMenus[1], "\pAbout \"Display a File\";(-");
- AddResMenu (myMenus[1], 'DRVR');
-
- //• File menu with open, close and quit selections.
- myMenus[2] = NewMenu (fileMenu, "\pFile");
- AppendMenu (myMenus[2], "\pOpen/O;(-;Close/C;(-;Quit/Q");
-
- //• Edit menu with cut, copy and paste.
- myMenus[3] = NewMenu (editMenu, "\pEdit");
- AppendMenu (myMenus[3], "\pUndo;(-;Cut;Copy;Paste");
- AppendMenu (myMenus[3], "\pClear;(-;Show Clipboard");
-
- //• Stop scroll menu.
- myMenus[4] = NewMenu (stopMenu, "\pClick Here to Pause Printout");
- AppendMenu (myMenus[4], "\pRelease mouse button to resume printout");
- for (i = 1; i <= lastMenu; i++)
- InsertMenu (myMenus[i], 0);
-
- //• Draw the completed menu bar.
- DrawMenuBar ();
- }
-
- //•---------------------------------------------------------------------------*/
- //• Center a string in the window.
-
- void Center (char *str)
- {
- Move (((howWide - StringWidth ((StringPtr) str)) / 2), 0);
- DrawString ((StringPtr) str);
- Move (-(myWindow->pnLoc.h), (myWindow->txSize) + 2);
- }
-
- //•---------------------------------------------------------------------------*/
- //• Draw a Pretty design.
-
- Pretty ()
- {
- short j;
- Rect tmpRec;
-
- BlockMove (&pRect, &tmpRec, (long)sizeof pRect);
- for (j = 0;j < 12; j++)
- {
- FrameOval (&tmpRec);
- InsetRect (&tmpRec, 6, 0);
- }
- BlockMove (&pRect, &tmpRec, (long)sizeof pRect);
- for (j = 0;j < 9; j++)
- {
- FrameOval (&tmpRec);
- InsetRect (&tmpRec, 0, 6);
- }
- }
-
-
-